package com.bawei.livedata.net;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import android.os.Bundle;
import android.widget.Toast;

import com.bawei.livedata.R;
import com.bawei.livedata.entity.BannerEntity;
import com.bawei.livedata.entity.BaseResponseEntity;

/**
 1。HttpUrlConnection
 2。Okhttp
 3。Retrofit+OkHttp+Rxjava：mvp     RxJava2CallAdapterFactory  依赖
 4。Retrofit+Okhttp+LiveData：mvvm  LiveDataCallAdapterFactory 自己封装
 5。Retrofit+Okhttp+协程+flow流：mvi
 */
public class NetActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net);

        Api api = RetrofitManager.getInstance().getRetrofit()
                .create(Api.class);
//        //retrofit返回Call--》更新UI还需要手动切换到主线程
//        api.banner(0,1,10).enqueue(new Callback<BaseResponseEntity<BannerEntity>>() {
//            @Override
//            public void onResponse(Call<BaseResponseEntity<BannerEntity>> call, Response<BaseResponseEntity<BannerEntity>> response) {
//                BannerEntity data = response.body().getData();
//                Toast.makeText(NetActivity.this,data.toString(),Toast.LENGTH_LONG).show();
//            }
//
//            @Override
//            public void onFailure(Call<BaseResponseEntity<BannerEntity>> call, Throwable t) {
//
//            }
//        });
        //retrofit结合rxjava返回Observable---》线程自由调度
//        api.banner(0,1,10)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<BaseResponseEntity<BannerEntity>>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//
//                    }
//
//                    @Override
//                    public void onNext(BaseResponseEntity<BannerEntity> bannerEntityBaseResponseEntity) {
//                        BannerEntity data = bannerEntityBaseResponseEntity.getData();
//                        Toast.makeText(NetActivity.this,data.toString(),Toast.LENGTH_LONG).show();
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });
        //liveData
        api.banner(0,1,10).observe(this, new Observer<BaseResponseEntity<BannerEntity>>() {
            @Override
            public void onChanged(BaseResponseEntity<BannerEntity> bannerEntityBaseResponseEntity) {
                BannerEntity data = bannerEntityBaseResponseEntity.getData();
                Toast.makeText(NetActivity.this,data.toString(),Toast.LENGTH_LONG).show();
            }
        });

    }
}